<?php

namespace App\Http\Controllers\Admin;

use App\Http\Query\CategoryQuery;
use app\Model\Base;
use app\Model\OrderModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Log;
use function MongoDB\BSON\toJSON;
use SplFileObject;

class JapanGoodsSeilController extends Controller

{

    function goods_seil_rank(Request $request)
    {

        $getParam = $request->all();
        $map = [];

        $db = DB::table('user_order_detailed as uod')
            ->select(DB::raw( 'DISTINCT gm.goods_name,gm.goods_id,cm.category_name,cm.category_id'))
            ->leftJoin('goods_master as gm', 'gm.goods_id', '=', 'uod.goods_id')
            ->leftJoin('goods_model as gmm', 'gm.goods_id',  DB::raw('gmm.goods_id and gmm.goods_model_id = uod.model_id'))
            ->leftJoin('category_master as cm', 'cm.category_id', '=', 'gm.category_id')
            ->where($map);

        if (!empty($getParam['star_date']) && !empty($getParam['end_date'])) {
            $list = $db->whereBetween('uod.create_time', [$getParam['star_date'], $getParam['end_date']])->get();
        } else {
            $list = $db->get();
        }

        $linData = [];
        foreach ($list as $key => $value) {
            $goods = DB::table('user_order_detailed')
                ->select(DB::raw('sum(goods_num) as goods_num,sum(goods_price) as goods_price'))
                ->where('goods_id', '=', $value->goods_id)
                ->first();
            $list[$key]->goods_num = $goods->goods_num;
            $list[$key]->goods_price = $goods->goods_price;

            $linData[$value->category_id][] = $value;
        }



        return view('warehouse.goods_seil_rank', array(
            'list' => $linData,
            'param'=>$getParam
        )); // 返回视图

    }
}
